<template>
	<view class="bg-white" style="width: 100%;height: 100%;">
		<cu-custom v-if="'header'+showHeader" :key="headerKey" bgColor="bg-gradual-red-yellow" :isBack="true" backText="返回" :title="title"></cu-custom>
		<web-view :src="url" v-if="iOS&&url"></web-view>
		<image v-if="imgage&&url" :src="url" style="width: 100%;height: 100%;" mode="aspectFill"></image>
		<video v-if="video&&url"
			class="video"
			:src="url"
			controls
			objectFit="fill"
			:autoplay="false"
		></video>
		<view v-if="!url" class="text-center text-gray text-lg">未找到查看的文件</view>
	</view>
</template>

<script>
	import api from '@/api/system/article.js';
	import store from '@/store';
	export default {
		onShow() {
			this.headerKey += 1;
		},
		data() {
			return {
				headerKey:0,
				title:'文章详情',
				iOS:false,
				imgage:false,
				video:false,
				showHeader:true,
				contentType:'richText',
				content:``,
				url:"",
				info:{},
				
			}
		},
		onLoad: function (option) {
			this.info=option;
			this.title =option.title
			if(option.contentType){
				this.contentType =option.contentType
				if(this.contentType == 'file'){
					this.url = option.fileUrl
					if(option.fileType=='pdf'){
						this.showPdf()
					} else if(option.fileType=='imgage'){
						this.imgage = true
					}else if(option.fileType=='video'){
						this.video = true
					}
				}
			}
		},
		methods: {
			showPdf(){
				
				// 判断操作系统
				    uni.getSystemInfo({
				      success: (res) => {
				        if (res.system.includes('iOS')) {
							uni.setNavigationBarTitle({
								title: '详情'
							})
							uni.setNavigationBarColor({
							    frontColor: '#ffffff',
							    backgroundColor: '#ed7d8c',
							    animation: {
							        duration: 50,
							        timingFunc: 'easeIn'
							    }
							})
							this.iOS = true
							this.showHeader=false
				        } else {
				          this.iOS = false
						  
				          // Android 需要下载后、打开
				          uni.downloadFile({
				            url: this.url,
				            success: (res) => {
				              const path = res.tempFilePath
				              uni.openDocument({
				                filePath: path,
				                fileType: 'pdf',
				                success: (res) => {
				                  uni.navigateBack({
				                    delta: 1
				                  });
				                },
				                fail: (err) => {
				                  uni.showToast({ title: '打开文件失败', icon: 'none', duration: 2000 });
				                }
				              });
				            },
				            fail: (err) => {
				              console.log(err);
				              uni.showToast({ title: '下载文件失败', icon: 'none', duration: 2000 });
				            }
				          });
				        }
				      }
				    });
			}
		}
	}
</script>

<style>

</style>
